LeetCode10

LeetCode 第10题的分析和总结

题目描述:

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

思路:

  首先确定是动态规划的匹配性的问题

  其次,确定问题的分解dp[i][j] 标识的是if s.substring(0,i) is valid for pattern p.substring(0,j) 这步是最困难的点。

  最后确定dp[i][j] 和 dp[i-1][j-1] 等之间的转移条件:
  if(p[j] == s[i]) dp[i][j] = dp[i-1][j-1];//①

  If(p[j]== '.')   dp[i][j] = dp[i-1][j-1];//②

  if(p[j]== '*') 情况比较的复杂了,分开进行讨论://③
    if( p[j-1] != s[i]) dp[i][j] = dp[i][j-2],举例说明的话,ab* 只能是匹配的a,不能是ac

    if( p[j-1] == s[i] or p[j-1] == '.')
      dp[i][j] = dp[i-1][j] // a* 匹配 aaaa
      or dp[i][j] = dp[i][j-1] // a* 匹配 a
      or dp[i][j] = dp[i][j-2] // a* 匹配 empty

代码:

public boolean isMatch(String s, String p) {
        if(s == null || p == null) {
            return false;
        }
        boolean[][] state = new boolean[s.length() + 1][p.length() + 1];
        state[0][0] = true;
        // no need to initialize state[i][0] as false  initialize state[0][j]
        //应用的条件是③
        for (int j = 1; j < state[0].length; j++) {
            if (p.charAt(j - 1) == '*') {
                if (state[0][j - 1] || (j > 1 && state[0][j - 2])) {
                    state[0][j] = true;
                }
            }
        }
        // 索引的范围是从1到length,标识的是0 标识的是null,第一个字符的下标是1,所以当前值对应的字符中的下标为i-1,j-1
        for (int i = 1; i < state.length; i++) {
            for (int j = 1; j < state[0].length; j++) {
                // 上面说明的转移条件①和②
                if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
                    state[i][j] = state[i - 1][j - 1];
                }
                // 上面说明的转移条件③
                if (p.charAt(j - 1) == '*') {
                    //这个就是标识 ,不适用③的前两个条件的内容:cb 匹配 cba*
                    if (s.charAt(i - 1) != p.charAt(j - 2) && p.charAt(j - 2) != '.') {
                        state[i][j] = state[i][j - 2];
                    } else {
                        //③中条件的完美的展示
                        state[i][j] = state[i - 1][j] || state[i][j - 1] || state[i][j - 2];
                    }
                }
            }
        }
        return state[s.length()][p.length()];
    }
}

Powered by andiHappy and Theme by AndiHappy